package com.migu.contacts.util;

import android.text.TextUtils;

import com.frank.libandroid.GsonUtil;
import com.google.gson.reflect.TypeToken;
import com.migu.contacts.bean.LabelBean;
import com.tencent.mmkv.MMKV;

import java.util.ArrayList;
import java.util.List;

public class LabelUtil {
    public static String getName(int id) {
        for (LabelBean groupBean : list) {
            if (groupBean.id == id) {
                return groupBean.name;
            }
        }
        return "";
    }

    public static int getId(String groupName) {
        for (LabelBean groupBean : list) {
            if (groupBean.name.equals(groupName)) {
                return groupBean.id;
            }
        }
        return -1;
    }

    private static List<LabelBean> list = new ArrayList<>();

    static {
        String string = MMKV.defaultMMKV().getString("label", "");
        if (!TextUtils.isEmpty(string)) {
            List<LabelBean> o = GsonUtil.fromJson(string, new TypeToken<List<LabelBean>>() {
            }.getType());
            list.addAll(o);
        }
    }

    public static void setLabels(List<LabelBean> groupBeans) {
        list.clear();
        list.addAll(groupBeans);
        MMKV.defaultMMKV().putString("label", GsonUtil.toJson(groupBeans));
    }

    public static LabelBean getBean(int id) {
        for (LabelBean groupBean : list) {
            if (groupBean.id == id) {
                return groupBean;
            }
        }
        return null;
    }
}
